class Admin::MediasController < Admin  
  # GET /medias
  # GET /medias.xml
  def index
    @medias = Media.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @medias }
    end
  end

  # GET /medias/1
  # GET /medias/1.xml
  def show
    @media = Media.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @media }
    end
  end

  def new
    @media = Media.new

    if(params[:artwork_id])
      @artwork = Artwork.find(params[:artwork_id])

      respond_to do |format|
        format.html { render :action => 'new_artwork' }
        format.xml  { render :xml => @media }
      end

    elsif(params[:artist_id])
      @artist = Artist.find(params[:artist_id])

      respond_to do |format|
        format.html { render :action => 'new_artist' }
        format.xml  { render :xml => @media }
      end
    else
      raise
    end  
  end

  # GET /medias/1/edit
  def edit
    @media = Media.find(params[:id])
  end

  # POST /medias
  # POST /medias.xml
  def create
    case params[:media][:media_type]
      when TYPE_PICTURE
        @media = Picture.new(params[:media])
      when TYPE_AUDIO
        @media = Audio.new(params[:media])
      when TYPE_VIDEO
         @media = Video.new(params[:media])
     else
       raise 
    end
   
    puts @media.inspect
    

    respond_to do |format|
      if @media.save
        flash[:notice] = Media.l_attr('successful creation')
        format.html { redirect_to :action => 'new' }
        format.xml  { render :xml => @media, :status => :created, :location => @media }
      else
        if @media.artwork_id.nil? # load artist version
          @artist = Artist.find(params[:artist_id])
          @artist.last_edited_by = current_user.id
          @artist.save

          format.html { redirect_to :action => 'new' }
          #format.html { render :action => "new_artist" }
        else # load artwork version
          @artwork = Artwork.find(params[:artwork_id])
          @artwork.last_edited_by = current_user.id
          @artwork.save
          
          format.html { redirect_to :action => 'new' }
          #format.html { render :action => "new_artwork" }
        end

        format.xml  { render :xml => @media.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def add # called from new artwork steps and edit artist; ajax
   case params[:media][:media_type].to_i
      when Media::TYPE_PICTURE
        @media = Picture.new(params[:media])
      when Media::TYPE_AUDIO
        @media = Audio.new(params[:media])
      when Media::TYPE_VIDEO
         @media = Video.new(params[:media])
     else
       puts params.inspect
       raise 
    end
    unless params[:region_id].nil?
      @media.region_id = params[:region_id]
    end
    unless params[:artist_id].nil?
      @media.artist_id = params[:artist_id]
      artist = Artist.find_by_id(params[:artist_id])
      artist.last_edited_by = current_user
      artist.save
    end
    unless params[:artwork_id].nil?
      @media.artwork_id = params[:artwork_id]
      artwork = Artwork.find_by_id(params[:artwork_id])
      artwork.last_edited_by = current_user
      artwork.save
    end
     
   #@media.file = params[:media][:file]
    @media.caption = params[:caption]
   
    respond_to do |format|
      if @media.save
        #format.html { render :action => "edit" }
        #format.html { redirect_to edit_admin_artist_path(@media.artist_id) + '#media' }
        format.html { redirect_to :back }
      else
        # TODO: error
        flash[:error] = 'Error: Media NOT uploaded!'
        format.html { redirect_to :back }
      end
    end 
  end
    
    
  
 
  # PUT /medias/1
  # PUT /medias/1.xml
  def update
    @media = Media.find(params[:id])

    respond_to do |format|
      if @media.update_attributes(params[:media])
        flash[:notice] = 'Media was successfully updated.'
        format.html { redirect_to(@media) }
        format.xml  { head :ok }
        format.js { render :update do |page|
          page.visual_effect :highlight, 'caption_' + @media.id.to_s
        end }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @media.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /medias/1
  # DELETE /medias/1.xml
  def destroy
    @media = Media.find(params[:id])
    @media.destroy
    @media.artwork.unpublish_if_necessary if @media.artwork
    @media.artist.unpublish_if_necessary if @media.artist
    
    respond_to do |format|
      format.html { redirect_to(medias_url) }
      format.xml  { head :ok }
      format.js { render :update do |page|
        page.remove "media_#{params[:id]}_a"
        page.remove "media_#{params[:id]}_b"
        page.visual_effect :highlight, 'media_table', :duration => 2
      end }
    end
  end
  
end
